from config.sessings import Config
from create_app import create_app
from utils.myjwt import mjwt
from flask import jsonify
import time
from flask import request

app = create_app(Config)

@app.before_request
def before_request():
    # 设置一个白名单
    whiltelist = ['/img','/sendSmsCode','/login']
    url = request.path
    print(url)
    if url not in whiltelist:
        # 判断当前访问的url是否存在白名单中，如果不在验证token
        try:
            token = request.headers['token']
            # 获取token
            #调用jwt封装好的checj_token对比
            flag = mjwt.check_token(token)
            if flag == True:
                # 如果通过验证过期时间
                payload = mjwt.jwt_decode(token)
                new = time.time()
                tnow = payload['exp']
                if int(new) - int(tnow) > 3600:
                    return jsonify({
                        'code': 401,
                        'msg': 'token已过期'
                    })
            else:
                return jsonify({
                    'code':401,
                    'msg':'token被篡改'
                })
        except:
            return jsonify({'code': 401,'msg': '没获取到token'})


if __name__ == '__main__':
    app.run()
